package cn.easyutil.veteran.core.logger;

import cn.easyutil.veteran.core.error.CommonException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.LoggerFactory;

import java.util.*;

/** 日志 */
public class LoggerUtil {

	//保存tranceId
	private static final ThreadLocal<String> tranceId = new ThreadLocal<>();

	//日志打印拦截器
	private static final List<LoggerPrintInterceptor> interceptors = new ArrayList<>();

	//如果没有日志拦截器，就返回全部打印
	private static final List<LoggerPrintInterceptor> defaultInterceptors = new ArrayList<>(Collections.singletonList((level, clazz, str, args) -> true));

	private static String getPre(){
		String app = System.getProperty("@appId");
		if(StringUtils.isBlank(app)){
			return "["+getTranceId()+"]>>>";
		}
		return "["+System.getProperty("@appId")+"]["+getTranceId()+"]>>>";
	}

	public static void debug(Class<?> clazz, String str, Object... objs) {
		boolean match = getInterceptors().stream().anyMatch(inc -> inc.isPrint(LogLevel.DEBUG, clazz, str, objs));
		if(match){
			compelDebug(clazz, str, objs);
		}
	}

	public static void compelDebug(Class<?> clazz, String str, Object... objs){
		str = str == null ? "" : str;
		LoggerFactory.getLogger(clazz).debug(getPre() + str, objs);
	}

	public static void info(Class<?> clazz, String str, Object... objs) {
		boolean match = getInterceptors().stream().anyMatch(inc -> inc.isPrint(LogLevel.INFO, clazz, str, objs));
		if(match){
			compelInfo(clazz, str, objs);
		}
	}

	public static void compelInfo(Class<?> clazz, String str, Object... objs){
		str = str == null ? "" : str;
		LoggerFactory.getLogger(clazz).info(getPre() + str, objs);
	}

	public static void warn(Class<?> clazz, String str, Object... objs) {
		boolean match = getInterceptors().stream().anyMatch(inc -> inc.isPrint(LogLevel.WARN, clazz, str, objs));
		if(match){
			compelWarn(clazz, str, objs);
		}
	}

	public static void compelWarn(Class<?> clazz, String str, Object... objs){
		str = str == null ? "" : str;
		LoggerFactory.getLogger(clazz).warn(getPre() + str, objs);
	}

	public static void error(Class<?> clazz,Throwable e, String str) {
		str = str == null ? "" : str;
		LoggerFactory.getLogger(clazz).error(getPre() + str, e);
	}

	public static void error(Class<?> clazz, Throwable e) {
		error(clazz,e, "");
	}

	public static void error(Class<?> clazz,String str){
		error(clazz,new CommonException(str));
	}

	public static void setTranceId(String tranceId) {
		LoggerUtil.tranceId.set(tranceId);
	}

	public static String getTranceId() {
		if(tranceId.get() == null){
			tranceId.set(UUID.randomUUID().toString().replace("-",""));
		}
		return tranceId.get();
	}

	public static void clearTransId(){
		tranceId.remove();
	}

	public static synchronized void addInterceptor(LoggerPrintInterceptor interceptor){
		if(interceptor != null){
			interceptors.add(interceptor);
		}
	}

	public static List<LoggerPrintInterceptor> getInterceptors() {
		if(interceptors.isEmpty()){
			return defaultInterceptors;
		}
		return interceptors;
	}
}
